#pragma once

#include <math.h>
#include "BicubicUniformPeriodicBSplinePatch.h"
#include "Exceptions.h"

#define PI 3.1415926535897932384626433832795

namespace cagd
{
    class BSplineCylinder: public BicubicUniformPeriodicBSplinePatch
    {
    public:
        BSplineCylinder(GLuint n, GLuint m, GLdouble a, GLdouble b, GLdouble r)
        {
            if (a >= b || r <= 0 || n < 3 || m < 3)
                throw Exception("Invalid BSplineCylinder parameters.");

            _control_net.ResizeRows(n);
            _control_net.ResizeColumns(m + 3);

            GLdouble u, v;
            for (GLuint i = 0; i < n; i++)
            {
                u = a + i * (b - a) / n;
                for (GLuint j = 0; j < m; j++)
                {
                    v = 2 * j * PI / m;

                    GLdouble x = r * cos(v), y = r * sin(v), z = u;
                    DCoordinate dc(x, y, z);
                    _control_net.SetElement(i, j, dc);
                }
            }

            for (GLuint i = 0; i < n; i++)
            {
                for (GLuint j = m; j < m + 3; j++)
                {
                    _control_net.SetElement(i, j, _control_net(i, j - m));
                }
            }
        }
    };
}
